<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\InsideTaskReviewWorkflowService;
use common\service\PlanService;
use common\service\TaskLibraryTaskMemberService;
use common\service\TaskLibraryTaskWorkOrderUserService;
use common\service\TaskLibraryV2Service;
use common\service\TodoService;
use think\Exception;
use think\Request;
use think\Response;

class TaskLibraryV2Controller extends VerifyController
{

    /**
     * @var TaskLibraryV2Service
     */
    private $taskLibraryV2Service;
    private $taskLibraryTaskMemberService;
    private $taskLibraryTaskWorkOrderUserService;

    public function __construct()
    {
        parent::__construct();
        $this->taskLibraryV2Service = TaskLibraryV2Service::getInstance();
        $this->taskLibraryTaskMemberService = TaskLibraryTaskMemberService::getInstance();
        $this->taskLibraryTaskWorkOrderUserService = TaskLibraryTaskWorkOrderUserService::getInstance();
    }

    /**
     * 任务库V2列表查询
     * @param Request $request
     * @return Response
     */
    public function list(Request $request)
    {
        $param = $request->param();
        $res = $this->taskLibraryV2Service->list($param, $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 分组列表
     * @param Request $request
     * @return Response
     */
    public function groupList(Request $request)
    {
        $param = $request->param();
        $res = $this->taskLibraryV2Service->groupList($param, $request->getTenantId(), $request->getUserId());
        return json(success_response('', $res));
    }

    /**
     * 异常情况详情
     * @param Request $request
     * @return Response
     */
    public function abnormalDetail(Request $request)
    {
        $param = $request->param()['param'];
        $taskIds = $param['filter']['task_ids'] ?? null;
        $res = $this->taskLibraryV2Service->abnormalDetail($taskIds);
        return json(success_response('', $res));
    }

    /**
     * 审核进度
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function reviewProgress(Request $request)
    {
        $param = $request->param();
        $taskIds = $param['param']['filter']['task_ids'] ?? null;
        $res = $this->taskLibraryV2Service->reviewProgress($taskIds, $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 查看任务延期情况
     * @param Request $request
     * @return Response
     */
    public function taskDelayInfo(Request $request)
    {
        $param = $request->param()['param'];
        $taskIds = $param['filter']['task_ids'] ?? null;
        $res = $this->taskLibraryV2Service->taskDelayInfo($taskIds);
        return json(success_response('', array_values($res)));
    }

    /**
     * 排期时间查询
     * @param Request $request
     * @return Response
     */
    public function getEpisodeStepCategoryPlanList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'];
        $episodeIds = $filter['entity_ids'] ?? null;
        $stepCategoryIds = $filter['link_ids'] ?? null;
        $fields = $param['fields'] ?? 'plan.*,plan.entity_id as episode_id,plan.end_time,s_tag.link_id';
        $res = PlanService::getInstance()->getEpisodeStepCategoryPlanList($episodeIds, $stepCategoryIds, $fields);
        return json(success_response('', $res));
    }

    /**
     * 批量修改工时
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function bathUpdateManualManHour(Request $request)
    {
        $param = $request->param()['data_list'];
        $res = $this->taskLibraryV2Service->bathUpdateManualManHour($param);
        return json(success_response('', $res));
    }


    /**
     * 获取任务详情列表json转数组
     * @param Request $request
     * @return Response
     */
    public function getTaskInfoList(Request $request)
    {
        $param = $request->param()['param'];
        if (is_string($param['fields'])) {
            $param['fields'] = explode(',', $param['fields']);
        }
        $res = $this->taskLibraryV2Service->getTaskInfoList($param);
        return json(success_response('', $res));
    }


    /**
     * 获取实体详情列表json转数组
     * @param Request $request
     * @return Response
     */
    public function getEntityInfoList(Request $request)
    {
        $param = $request->param()['param'];
        if (is_string($param['fields'])) {
            $param['fields'] = explode(',', $param['fields']);
        }
        $res = $this->taskLibraryV2Service->getEntityInfoList($param);
        return json(success_response('', $res));
    }

    /**
     * 审核中
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function reviewIng(Request $request)
    {
        $param = $request->param();
        $filter = $param['param']['filter'];
        $res = $this->taskLibraryV2Service->reviewIng($filter['task_ids']);
        return json(success_response('', $res));
    }

    /**
     * 审核进度详情
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function reviewRequestProgressList(Request $request)
    {
        $param = $request->param();
        $filter = $param['param']['filter'];
        $res = $this->taskLibraryV2Service->reviewRequestProgressList($filter['review_request_id']);
        return json(success_response('', $res));
    }

    /**
     * 批量修改制作时间
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function bathUpdateTaskScheduleDate(Request $request)
    {
        $param = $request->param();
        $res = $this->taskLibraryV2Service->bathUpdateTaskScheduleDate($param);
        return json(success_response('', $res));
    }

    /**
     * 代办任务列表
     * @param Request $request
     * @return Response
     */
    public function todoList(Request $request)
    {
        $param = $request->param();
        $res = $this->taskLibraryV2Service->todoList($param, $request->getUserId(), $request->getTenantId());
        return json(success_response('', $res));
    }

    /**
     * 获取代办计划交付时间
     * @param Request $request
     * @return Response
     */
    public function getPlanDeliveryTime(Request $request)
    {
        $param = $request->param();
        $filter = $param['param']['filter'];
        $taskIdList = $filter['id'] ?? [];
        $planDeliverTimeData = TodoService::getInstance()->getPlanDeliveryTime($taskIdList);
        $returnData = [];
        foreach ($planDeliverTimeData as $taskId => $item) {
            if (empty($item)) {
                continue;
            }
            $item['relation_task_id'] = $item['task_id'];
            $item['task_id'] = $taskId;
            $returnData[] = $item;
        }
        return json(success_response('', $returnData));
    }

    /**
     * 获取镜头时长
     * @param Request $request
     * @return Response
     */
    public function getStoryboardEstimateDuration(Request $request)
    {
        $param = $request->param();
        $filter = $param['param']['filter'];
        $res = $this->taskLibraryV2Service->getStoryboardEstimateDuration($filter);
        return json(success_response('', $res));
    }

    /**
     * 通过任务id,找到关联环节
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getTaskRelationTagStepCategory(Request $request)
    {
        $param = $request->param();
        $taskIds = $param['param']['task_ids'] ?? '';
        $res = $this->taskLibraryV2Service->getTaskRelationTagStepCategory($taskIds);
        return json(success_response('', $res));
    }

    /**
     * 用户成员任务查询
     * @param Request $request
     * @return Response
     */
    public function getTaskMemberTaskList(Request $request): Response
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, C('default_page_total')];
        $res = $this->taskLibraryTaskMemberService->getTaskIdList($param, $page);
        return json(success_response('', $res));
    }

    /**
     * 与我相关统计
     * @param Request $request
     * @return Response
     */
    public function getTaskMemberCount(Request $request): Response
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, 100];
        $code = $param['code'] ?? '';
        $filter = $page['filter'] ?? [];
        $res = $this->taskLibraryTaskMemberService->getTaskMemberCount($code, $filter, $page);
        return json(success_response('', $res));
    }

    /**
     * 查询工单用户任务列表
     * @param Request $request
     * @return Response
     */
    public function getTaskWorkOrderUserList(Request $request): Response
    {
        $param = $request->param()['param'];
        $fields = $param['fields'] ?? [];
        $userId = $param['user_id'] ?? $request->getUserId();
        $page = $param['page'] ?? [1, C('default_page_total')];
        $res = $this->taskLibraryTaskWorkOrderUserService->getTaskWorkOrderUserList($userId, $fields, $page);
        return json(success_response('', $res));
    }

    /**
     * 获取多个任务属性
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getTaskAttrList(Request $request): Response
    {
        $param = $request->param()['param'];
        $taskIdList = $param['task_id_list'] ?? [];
        $returnList = [];
        $res = InsideTaskReviewWorkflowService::getInstance()->getMultipleTaskAttr($taskIdList);
        foreach ($res as $taskId => $attr) {
            $returnList[] = [
                'task_id' => $taskId,
                'task_attr' => $attr
            ];
        }
        return json(success_response('', $returnList));
    }

}
